import {call,put,takeEvery} from 'redux-saga/effects'
import fetch from 'isomorphic-fetch'

// const fetchFun=myparam=>{
//     var rejson=fetch(`https://www.reddit.com/r/${myparam}.json`)
//         .then(response=>response.json())
//         .then(json=>{return json});
//     return rejson;
// }
const fetchApi=async function(myparam){
    return await fetch(`https://www.reddit.com/r/${myparam}.json`).then(response=>response.json());
}

function* fetchData(action){
    const rejson=yield call(fetchApi,action.payload.selectReddit)

    yield put ({type:"RECEIVE_POSTS",posts:rejson.data.children.map(child=>child.data)})
}

function* mySaga(){
    yield takeEvery("REQUEST_POSTS",fetchData)
}

export default mySaga